package com.softtown.modules.dapai.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompCustomerAppDao;
import com.softtown.modules.dapai.dao.CompCustomerDao;
import com.softtown.modules.dapai.dto.CompCustomerDTO;
import com.softtown.modules.dapai.dto.CustomerAddAppDTO;
import com.softtown.modules.dapai.dto.CustomerAddDTO;
import com.softtown.modules.dapai.dto.CustomerAppDTO;
import com.softtown.modules.dapai.entity.*;
import com.softtown.modules.dapai.service.*;
import com.softtown.modules.dapai.vo.CompCustomerVO;
import com.softtown.modules.dapai.vo.CustomerContactsVO;
import com.softtown.modules.oss.cloud.OSSFactory;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.dto.CompComDocAttAddDTO;
import com.softtown.modules.sys.entity.CompComDocAttEntity;
import com.softtown.modules.sys.service.CompComDocAttService;
import com.softtown.modules.utils.DateUtil;
import com.softtown.modules.utils.Tools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service
public class CompCustomerAppServiceImpl extends
        BaseServiceImpl<CompCustomerAppDao, CompCustomerAppEntity> implements CompCustomerAppService {

    @Override
    @Transactional
    public void addCustomerApp(CustomerAddAppDTO customerAddAppDTO) {
        CompCustomerAppEntity compCustomerAppEntity = ConvertUtils
                .sourceToTarget(customerAddAppDTO, CompCustomerAppEntity.class);
        compCustomerAppEntity.setCustCode(getCode());
        baseDao.insert(compCustomerAppEntity);
    }

    @Override
    @Transactional
    public void updateCustomeApp(CustomerAddAppDTO customerAddAppDTO) {
        CompCustomerAppEntity compCustomerAppEntity = ConvertUtils
                .sourceToTarget(customerAddAppDTO, CompCustomerAppEntity.class);
        baseDao.updateById(compCustomerAppEntity);
    }

    @Override
    @Transactional
    public void deleteCustomerApp(Long compId, Long id) {
        CompCustomerAppEntity compCustomerAppEntity = baseDao.selectOne(new QueryWrapper<CompCustomerAppEntity>().lambda().eq(CompCustomerAppEntity ::getId,id)
                .eq(CompCustomerAppEntity::getCompId,compId));
        compCustomerAppEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        baseDao.updateById(compCustomerAppEntity);
    }

    @Override
    public PageInfo<CompCustomerAppEntity> getCustomerPageListApp(CustomerAppDTO customerAppDTO) {
        Integer pageSize = customerAppDTO.getPageSize();
        Integer page = customerAppDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompCustomerAppEntity> compCustomerAppEntities =
                baseDao.getCustomerPageListApp(customerAppDTO.getCompId(), customerAppDTO.getParam());
        return  new PageInfo<>(compCustomerAppEntities);
    }

    @Override
    public CompCustomerAppEntity getCustomerByIdApp(Long compId, Long id) {
        return baseDao.getCustomerByIdApp(compId, id);
    }

    @Override
    public String getCode(){
        String codeModel = "";
        int num = 1;//当前第几位
        String now_Dates_two = DateUtil.getYYMM(); // 年月 2023
        String preFix = "DPKH";//前缀  CG
        codeModel = preFix + now_Dates_two;
        CompCustomerAppEntity compCustomerAppEntity = baseDao.selectOne(new QueryWrapper<CompCustomerAppEntity>()
                .lambda().like(CompCustomerAppEntity::getCustCode,codeModel)
                .orderByDesc(CompCustomerAppEntity::getCustCode).last("limit 1"));
        if (compCustomerAppEntity != null) {
            String preCode = compCustomerAppEntity.getCustCode();
            num = Integer.parseInt(preCode.substring(codeModel.length(), preCode.length()));
            num++;
        }

        return codeModel + Tools.fillPrefix(String.valueOf(num), "0", 4);
    }
}
